home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS02.ADF / Emacs / window.c < prev    next >
C/C++ Source or Header  |  1989-05-30  |  12KB  |  381 lines

  1. /* window.c */
  2.  
  3.  
  4. /*
  5.  * Window management. Some of the functions are internal, and some are
  6.  * attached to keys that the user actually types.
  7.  */
  8.  
  9. #include        <stdio.h>
  10. #include        "ed.h"
  11.  
  12. /*
  13.  * Reposition dot in the current window to line "n". If the argument is
  14.  * positive, it is that line. If it is negative it is that line from the
  15.  * bottom. If it is 0 the window is centered (this is what the standard
  16.  * redisplay code does). With no argument it defaults to 1. Bound to M-!.
  17.  * Because of the default, it works like in Gosling.
  18.  */
  19. reposition(f, n)
  20.     {
  21.     curwp->w_force = n;
  22.     curwp->w_flag |= WFFORCE;
  23.     return (TRUE);
  24.     }
  25.  
  26. /*
  27.  * Refresh the screen. With no argument, it just does the refresh. With an
  28.  * argument it recenters "." in the current window. Bound to "C-L".
  29.  */
  30. refresh(f, n)
  31.     {
  32.     if (f == FALSE)
  33.         sgarbf = TRUE;
  34.     else
  35.         {
  36.         curwp->w_force = 0;             /* Center dot. */
  37.         curwp->w_flag |= WFFORCE;
  38.         }
  39.  
  40.     return (TRUE);
  41.     }
  42.  
  43. /*
  44.  * The command make the next window (next => down the screen) the current
  45.  * window. There are no real errors, although the command does nothing if
  46.  * there is only 1 window on the screen. Bound to "C-X C-N".
  47.  */
  48. nextwind(f, n)
  49.     {
  50.     register WINDOW *wp;
  51.  
  52.     if ((wp = curwp->w_wndp) == NULL)
  53.         wp = wheadp;
  54.  
  55.     curwp = wp;
  56.     curbp = wp->w_bufp;
  57.     return (TRUE);
  58.     }
  59.  
  60. /*
  61.  * This command makes the previous window (previous => up the screen) the
  62.  * current window. There arn't any errors, although the command does not do a
  63.  * lot if there is 1 window.
  64.  */
  65. prevwind(f, n)
  66.     {
  67.     register WINDOW *wp1;
  68.     register WINDOW *wp2;
  69.  
  70.     wp1 = wheadp;
  71.     wp2 = curwp;
  72.  
  73.     if (wp1 == wp2)
  74.         wp2 = NULL;
  75.  
  76.     while (wp1->w_wndp != wp2)
  77.         wp1 = wp1->w_wndp;
  78.  
  79.     curwp = wp1;
  80.     curbp = wp1->w_bufp;
  81.     return (TRUE);
  82.     }
  83.  
  84. /*
  85.  * This command moves the current window down by "arg" lines. Recompute the
  86.  * top line in the window. The move up and move down code is almost completely
  87.  * the same; most of the work has to do with reframing the window, and picking
  88.  * a new dot. We share the code by having "move down" just be an interface to
  89.  * "move up". Magic. Bound to "C-X C-N".
  90.  */
  91. mvdnwind(f, n)
  92.     int n;
  93.     {
  94.     return (mvupwind(f, -n));
  95.     }
  96.  
  97. /*
  98.  * Move the current window up by "arg" lines. Recompute the new top line of
  99.  * the window. Look to see if "." is still on the screen. If it is, you win.
  100.  * If it isn't, then move "." to center it in the new framing of the window
  101.  * (this command does not really move "."; it moves the frame). Bound to
  102.  * "C-X C-P".
  103.  */
  104. mvupwind(f, n)
  105.     int n;
  106.     {
  107.     register LINE *lp;
  108.     register int i;
  109.  
  110.     lp = curwp->w_linep;
  111.  
  112.     if (n < 0)
  113.         {
  114.         while (n++ && lp!=curbp->b_linep)
  115.             lp = lforw(lp);
  116.         }
  117.     else
  118.         {
  119.         while (n-- && lback(lp)!=curbp->b_linep)
  120.             lp = lback(lp);
  121.         }
  122.  
  123.     curwp->w_linep = lp;
  124.     curwp->w_flag |= WFHARD;            /* Mode line is OK. */
  125.  
  126.     for (i = 0; i < curwp->w_ntrows; ++i)
  127.         {
  128.         if (lp == curwp->w_dotp)
  129.             return (TRUE);
  130.         if (lp == curbp->b_linep)
  131.             break;
  132.         lp = lforw(lp);
  133.         }
  134.  
  135.     lp = curwp->w_linep;
  136.     i  = curwp->w_ntrows/2;
  137.  
  138.     while (i-- && lp != curbp->b_linep)
  139.         lp = lforw(lp);
  140.  
  141.     curwp->w_dotp  = lp;
  142.     curwp->w_doto  = 0;
  143.     return (TRUE);
  144.     }
  145.  
  146. /*
  147.  * This command makes the current window the only window on the screen. Bound
  148.  * to "C-X 1". Try to set the framing so that "." does not have to move on the
  149.  * display. Some care has to be taken to keep the values of dot and mark in
  150.  * the buffer structures right if the distruction of a window makes a buffer
  151.  * become undisplayed.
  152.  */
  153. onlywind(f, n)
  154. {
  155.         register WINDOW *wp;
  156.         register LINE   *lp;
  157.         register int    i;
  158.  
  159.         while (wheadp != curwp) {
  160.                 wp = wheadp;
  161.                 wheadp = wp->w_wndp;
  162.                 if (--wp->w_bufp->b_nwnd == 0) {
  163.                         wp->w_bufp->b_dotp  = wp->w_dotp;
  164.                         wp->w_bufp->b_doto  = wp->w_doto;
  165.                         wp->w_bufp->b_markp = wp->w_markp;
  166.                         wp->w_bufp->b_marko = wp->w_marko;
  167.                 }
  168.                 free((char *) wp);
  169.         }
  170.         while (curwp->w_wndp != NULL) {
  171.                 wp = curwp->w_wndp;
  172.                 curwp->w_wndp = wp->w_wndp;
  173.                 if (--wp->w_bufp->b_nwnd == 0) {
  174.                         wp->w_bufp->b_dotp  = wp->w_dotp;
  175.                         wp->w_bufp->b_doto  = wp->w_doto;
  176.                         wp->w_bufp->b_markp = wp->w_markp;
  177.                         wp->w_bufp->b_marko = wp->w_marko;
  178.                 }
  179.                 free((char *) wp);
  180.         }
  181.         lp = curwp->w_linep;
  182.         i  = curwp->w_toprow;
  183.         while (i!=0 && lback(lp)!=curbp->b_linep) {
  184.                 --i;
  185.                 lp = lback(lp);
  186.         }
  187.         curwp->w_toprow = 0;
  188.         curwp->w_ntrows = term.t_nrow-1;
  189.         curwp->w_linep  = lp;
  190.         curwp->w_flag  |= WFMODE|WFHARD;
  191.         return (TRUE);
  192. }
  193.  
  194. /*
  195.  * Split the current window. A window smaller than 3 lines cannot be split.
  196.  * The only other error that is possible is a "malloc" failure allocating the
  197.  * structure for the new window. Bound to "C-X 2".
  198.  */
  199. splitwind(f, n)
  200. {
  201.         register WINDOW *wp;
  202.         register LINE   *lp;
  203.         register int    ntru;
  204.         register int    ntrl;
  205.         register int    ntrd;
  206.         register WINDOW *wp1;
  207.         register WINDOW *wp2;
  208.  
  209.         if (curwp->w_ntrows < 3) {
  210.                 mlwrite("Cannot split a %d line window", curwp->w_ntrows);
  211.                 return (FALSE);
  212.         }
  213.         if ((wp = (WINDOW *) malloc(sizeof(WINDOW))) == NULL) {
  214.                 mlwrite("Cannot allocate WINDOW block");
  215.                 return (FALSE);
  216.         }
  217.         ++curbp->b_nwnd;                        /* Displayed twice.     */
  218.         wp->w_bufp  = curbp;
  219.         wp->w_dotp  = curwp->w_dotp;
  220.         wp->w_doto  = curwp->w_doto;
  221.         wp->w_markp = curwp->w_markp;
  222.         wp->w_marko = curwp->w_marko;
  223.         wp->w_flag  = 0;
  224.         wp->w_force = 0;
  225.         ntru = (curwp->w_ntrows-1) / 2;         /* Upper size           */
  226.         ntrl = (curwp->w_ntrows-1) - ntru;      /* Lower size           */
  227.         lp = curwp->w_linep;
  228.         ntrd = 0;
  229.         while (lp != curwp->w_dotp) {
  230.                 ++ntrd;
  231.                 lp = lforw(lp);
  232.         }
  233.         lp = curwp->w_linep;
  234.         if (ntrd <= ntru) {                     /* Old is upper window. */
  235.                 if (ntrd == ntru)               /* Hit mode line.       */
  236.                         lp = lforw(lp);
  237.                 curwp->w_ntrows = ntru;
  238.                 wp->w_wndp = curwp->w_wndp;
  239.                 curwp->w_wndp = wp;
  240.                 wp->w_toprow = curwp->w_toprow+ntru+1;
  241.                 wp->w_ntrows = ntrl;
  242.         } else {                                /* Old is lower window  */
  243.                 wp1 = NULL;
  244.                 wp2 = wheadp;
  245.                 while (wp2 != curwp) {
  246.                         wp1 = wp2;
  247.                         wp2 = wp2->w_wndp;
  248.                 }
  249.                 if (wp1 == NULL)
  250.                         wheadp = wp;
  251.                 else
  252.                         wp1->w_wndp = wp;
  253.                 wp->w_wndp   = curwp;
  254.                 wp->w_toprow = curwp->w_toprow;
  255.                 wp->w_ntrows = ntru;
  256.                 ++ntru;                         /* Mode line.           */
  257.                 curwp->w_toprow += ntru;
  258.                 curwp->w_ntrows  = ntrl;
  259.                 while (ntru--)
  260.                         lp = lforw(lp);
  261.         }
  262.         curwp->w_linep = lp;                    /* Adjust the top lines */
  263.         wp->w_linep = lp;                       /* if necessary.        */
  264.         curwp->w_flag |= WFMODE|WFHARD;
  265.         wp->w_flag |= WFMODE|WFHARD;
  266.         return (TRUE);
  267. }
  268.  
  269. /*
  270.  * Enlarge the current window. Find the window that loses space. Make sure it
  271.  * is big enough. If so, hack the window descriptions, and ask redisplay to do
  272.  * all the hard work. You don't just set "force reframe" because dot would
  273.  * move. Bound to "C-X Z".
  274.  */
  275. enlargewind(f, n)
  276. {
  277.         register WINDOW *adjwp;
  278.         register LINE   *lp;
  279.         register int    i;
  280.  
  281.         if (n < 0)
  282.                 return (shrinkwind(f, -n));
  283.         if (wheadp->w_wndp == NULL) {
  284.                 mlwrite("Only one window");
  285.                 return (FALSE);
  286.         }
  287.         if ((adjwp=curwp->w_wndp) == NULL) {
  288.                 adjwp = wheadp;
  289.                 while (adjwp->w_wndp != curwp)
  290.                         adjwp = adjwp->w_wndp;
  291.         }
  292.         if (adjwp->w_ntrows <= n) {
  293.                 mlwrite("Impossible change");
  294.                 return (FALSE);
  295.         }
  296.         if (curwp->w_wndp == adjwp) {           /* Shrink below.        */
  297.                 lp = adjwp->w_linep;
  298.                 for (i=0; i<n && lp!=adjwp->w_bufp->b_linep; ++i)
  299.                         lp = lforw(lp);
  300.                 adjwp->w_linep  = lp;
  301.                 adjwp->w_toprow += n;
  302.         } else {                                /* Shrink above.        */
  303.                 lp = curwp->w_linep;
  304.                 for (i=0; i<n && lback(lp)!=curbp->b_linep; ++i)
  305.                         lp = lback(lp);
  306.                 curwp->w_linep  = lp;
  307.                 curwp->w_toprow -= n;
  308.         }
  309.         curwp->w_ntrows += n;
  310.         adjwp->w_ntrows -= n;
  311.         curwp->w_flag |= WFMODE|WFHARD;
  312.         adjwp->w_flag |= WFMODE|WFHARD;
  313.         return (TRUE);
  314. }
  315.  
  316. /*
  317.  * Shrink the current window. Find the window that gains space. Hack at the
  318.  * window descriptions. Ask the redisplay to do all the hard work. Bound to
  319.  * "C-X C-Z".
  320.  */
  321. shrinkwind(f, n)
  322. {
  323.         register WINDOW *adjwp;
  324.         register LINE   *lp;
  325.         register int    i;
  326.  
  327.         if (n < 0)
  328.                 return (enlargewind(f, -n));
  329.         if (wheadp->w_wndp == NULL) {
  330.                 mlwrite("Only one window");
  331.                 return (FALSE);
  332.         }
  333.         if ((adjwp=curwp->w_wndp) == NULL) {
  334.                 adjwp = wheadp;
  335.                 while (adjwp->w_wndp != curwp)
  336.                         adjwp = adjwp->w_wndp;
  337.         }
  338.         if (curwp->w_ntrows <= n) {
  339.                 mlwrite("Impossible change");
  340.                 return (FALSE);
  341.         }
  342.         if (curwp->w_wndp == adjwp) {           /* Grow below.          */
  343.                 lp = adjwp->w_linep;
  344.                 for (i=0; i<n && lback(lp)!=adjwp->w_bufp->b_linep; ++i)
  345.                         lp = lback(lp);
  346.                 adjwp->w_linep  = lp;
  347.                 adjwp->w_toprow -= n;
  348.         } else {                                /* Grow above.          */
  349.                 lp = curwp->w_linep;
  350.                 for (i=0; i<n && lp!=curbp->b_linep; ++i)
  351.                         lp = lforw(lp);
  352.                 curwp->w_linep  = lp;
  353.                 curwp->w_toprow += n;
  354.         }
  355.         curwp->w_ntrows -= n;
  356.         adjwp->w_ntrows += n;
  357.         curwp->w_flag |= WFMODE|WFHARD;
  358.         adjwp->w_flag |= WFMODE|WFHARD;
  359.         return (TRUE);
  360. }
  361.  
  362. /*
  363.  * Pick a window for a pop-up. Split the screen if there is only one window.
  364.  * Pick the uppermost window that isn't the current window. An LRU algorithm
  365.  * might be better. Return a pointer, or NULL on error.
  366.  */
  367. WINDOW  *
  368. wpopup()
  369. {
  370.         register WINDOW *wp;
  371.  
  372.         if (wheadp->w_wndp == NULL              /* Only 1 window        */
  373.         && splitwind(FALSE, 0) == FALSE)        /* and it won't split   */
  374.                 return (NULL);
  375.         wp = wheadp;                            /* Find window to use   */
  376.         while (wp!=NULL && wp==curwp)
  377.                 wp = wp->w_wndp;
  378.         return (wp);
  379. }
  380.  
  381.